package com.feel.controller;

import com.feel.exception.SteduException;
import com.feel.pojo.Department;
import com.feel.pojo.RespBean;
import com.feel.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 刘永钦
 * @Date: 2024-09-03-14:12
 * @Description:
 */
@RestController
@RequestMapping("/admin/department")
public class DepartmentControllar {
    @Autowired
    private DepartmentService departmentService;

    /**
     * 获得所有部门信息
     */
    @GetMapping
    RespBean getAll() {
        List<Department> list = departmentService.getAll();
        return RespBean.ok("查询成功", list);
    }
    /**
     * 更新业务信息
     */
    @PutMapping
    RespBean updateInfo(@RequestBody @Validated Department department) throws SteduException {
        Boolean b = departmentService.updateInfo(department);
        return b ? RespBean.ok("更新成功") : RespBean.error("更新失败");
    }

    /**
     * 添加业务
     */
    @PostMapping
    RespBean addInfo(@RequestBody @Validated Department department) throws SteduException {
        Boolean b = departmentService.addInfo(department);
        return b ? RespBean.ok("添加成功") : RespBean.error("添加失败");
    }
    /**
     * 删除业务
     */
    @DeleteMapping("/{id}")
    RespBean delInfo(@PathVariable("id") Integer id) throws SteduException {
        Boolean b = departmentService.delInfo(id);
        return b ? RespBean.ok("删除成功") : RespBean.error("删除失败");
    }
    /**
     * 通过id查询
     */
    @GetMapping("/{id}")
    RespBean getById(@PathVariable("id") Integer id) {
        Department department = departmentService.getByID(id);
        return RespBean.ok("查询成功", department);
    }

    /**
     * 修改部门的负责人
     */
    @PutMapping("/updateAdmin")
    RespBean updateAdmin(Integer oldId, Integer userId) throws SteduException {
        Boolean b =departmentService.updateAdmin(oldId,userId);
        return b ? RespBean.ok("修改成功") : RespBean.error("修改失败");
    }
}
